package cn.thoughtworks.school.controllers;

import cn.thoughtworks.school.entities.HomeworkSubmission;
import cn.thoughtworks.school.handlers.BusinessException;
import cn.thoughtworks.school.repositories.HomeworkSubmissionRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/api/v3/homeworkSubmission")
public class HomeworkSubmissionController {

    @Autowired
    private HomeworkSubmissionRepository homeworkSubmissionRepository;

    @Value("${jenkins-callback-status.progress}")
    private int progress;

    @RequestMapping(value = "/{id}/logs", method = RequestMethod.GET)
    public ResponseEntity getSubmissionHomeworkLogBySubmissionId(@PathVariable Long id) throws BusinessException {
        HomeworkSubmission homeworkSubmission = homeworkSubmissionRepository
                .findById(id)
                .orElseThrow(() -> new BusinessException(
                        String.format("Unknown homeworkSubmission with id: %s", id)
                ));

        return new ResponseEntity<>(homeworkSubmission, HttpStatus.OK);
    }

    @RequestMapping(value = "users/{userId}/assignments/{assignmentId}/quizzess/{quizId}/logs", method = RequestMethod.GET)
    public ResponseEntity getSubmissionHomeworkLogBySubmissionId(@PathVariable Long assignmentId,
                                                                 @PathVariable Long quizId,
                                                                 @PathVariable Long userId) throws BusinessException {
        ArrayList<HomeworkSubmission> homeworkSubmissions = homeworkSubmissionRepository.findByQuizIdAndAssignmentIdAndUserIdOrderByIdDesc(quizId, assignmentId, userId);
        if (homeworkSubmissions.size() > 0) {
            return new ResponseEntity(homeworkSubmissions.get(0), HttpStatus.OK);
        }
        return new ResponseEntity(HttpStatus.OK);
    }

    @RequestMapping(value = "/oldAssignments/{oldAssignmentId}/quizzes/{quizId}/newAssignments/{newAssignmentId}", method = RequestMethod.GET)
    public ResponseEntity homeworkDataMigration(@PathVariable Long oldAssignmentId, @PathVariable Long quizId, @PathVariable Long newAssignmentId) throws BusinessException {
        List<HomeworkSubmission> homeworkSubmissions = homeworkSubmissionRepository.findAllByAssignmentIdAndQuizId(oldAssignmentId, quizId);
        if (homeworkSubmissions.size() != 0) {
            for (HomeworkSubmission homeworkSubmission : homeworkSubmissions) {
                homeworkSubmission.setAssignmentId(newAssignmentId);
                homeworkSubmissionRepository.save(homeworkSubmission);
            }
        }

        return new ResponseEntity<>(HttpStatus.OK);
    }
}
